package com.villaday.idf.custom;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.graphics.drawable.ColorDrawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

import com.villaday.idf.R;
import com.villaday.idf.util.LogeUtil;
import com.villaday.idf.util.PhoneUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class SideBar extends View {
    // 触摸事件
    private OnTouchingLetterChangedListener onTouchingLetterChangedListener;
    public static String[] b = {"A", "B", "C", "D", "E", "F", "G", "H", "I",
            "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
            "W", "X", "Y", "Z", "#"};
    private int choose = -1;
    private Paint paint;

    private TextView mTextDialog;
    private Context mContext;
    float charWidthAndHeight;
    int finalWidth = 0, finalHeight = 0;

    /**
     * 为SideBar设置显示字母的TextView
     *
     * @param textDialog
     */
    public void setTextView(TextView textDialog) {
        this.mTextDialog = textDialog;
    }

    /**
     * 2018年10月2日16:17:53
     */
    // 字母的间距
    private int letterSpace = 15;
    // 字母列表
    private List<String> letter;
    private boolean isMeasure;

    public void setLetter(List<String> letter) {
        this.letter = letter;
        setBackground(new ColorDrawable(0x00000000));
        invalidate();
    }


    public SideBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.mContext = context;
        if (letter == null) {
            letter = new ArrayList<>();
            letter = Arrays.asList(b);
        }
        paint = new Paint();
        paint.setColor(Color.parseColor("#ff0000"));
        // paint.setColor(Color.WHITE);
        paint.setTypeface(Typeface.DEFAULT);
        paint.setAntiAlias(true);
        paint.setTextSize(PhoneUtil.dp2px(mContext, 14));


    }

    public SideBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SideBar(Context context) {
        this(context, null);
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
//        int finalWidth = 0, finalHeight = 0;
        // 测量单个字符的宽度和高度
         charWidthAndHeight = paint.measureText(letter.get(0)) + letterSpace;
        if (widthMode == MeasureSpec.EXACTLY) {
            finalWidth = MeasureSpec.getSize(widthMeasureSpec);
        } else if (widthMode == MeasureSpec.AT_MOST) {
            finalWidth = (int) charWidthAndHeight + getPaddingLeft() + getPaddingRight();
        }
        if (heightMode == MeasureSpec.EXACTLY) {
            finalHeight = MeasureSpec.getSize(heightMeasureSpec);
        } else if (heightMode == MeasureSpec.AT_MOST) {
            // 注意measureText的值与 paint.setTextSize的值有关
            finalHeight = (int) charWidthAndHeight * letter.size() + getPaddingBottom() + getPaddingTop();
            LogeUtil.e("getLetter", "charWidthAndHeight=" + charWidthAndHeight + " letter.get(0)=" + letter.get(0));
        }
        //        Log.e("--------------->", MeasureSpec.getSize(widthMeasureSpec) + "    " + MeasureSpec.getSize(heightMeasureSpec));
        setMeasuredDimension(finalWidth, finalHeight);
    }


    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int height = getHeight();
        int width = getWidth();
//        int singleHeight = height / b.length;// 获取每一个字母的高度
        int singleHeight = height / letter.size();// 获取每一个字母的高度
 /*       for (int i = 0; i < b.length; i++) {
            paint.setColor(Color.rgb(0xff, 00, 00));
            // paint.setColor(Color.WHITE);
            paint.setTypeface(Typeface.DEFAULT);
            paint.setAntiAlias(true);
            paint.setTextSize(PhoneUtil.dp2px(mContext, 14));
            if (i == choose) {// 选中的状态
                paint.setColor(Color.parseColor("#3399ff"));
                paint.setFakeBoldText(true);
            }
            // x坐标等于中间-字符串宽度的一半
            float xPos = width / 2 - paint.measureText(b[i]) / 2;
            float yPos = singleHeight * i + singleHeight;
            canvas.drawText(b[i], xPos, yPos, paint);
            paint.reset();
        }*/
        float yPos = getHeight() / letter.size();
        LogeUtil.e("getLetter", "yPos==" + yPos + "   letter.size()=" + letter.size() + " ggetHeight()=" + getHeight());
        Paint.FontMetricsInt fontMetricsInt = paint.getFontMetricsInt();
        for (int i = 0; i < letter.size(); i++) {
//            if (i == choose) {// 选中的状态
//                paint.setColor(Color.parseColor("#3399ff"));
//                paint.setFakeBoldText(true);
//            }
            // x坐标等于中间-字符串宽度的一半
            float xPos = width / 2 - paint.measureText(letter.get(i)) / 2;
//
            float correctY = ((yPos * i + yPos) + (yPos * i) - fontMetricsInt.bottom - fontMetricsInt.top) / 2;
//            canvas.drawText(letter.get(i), xPos, ((yPos * i) + (yPos * i + yPos)) / 2, paint);
            canvas.drawText(letter.get(i), xPos, (singleHeight * i + singleHeight), paint);
//            paint.reset();
        }

    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        final int action = event.getAction();
        final float y = event.getY();
        final int oldChoose = choose;
        final OnTouchingLetterChangedListener listener = onTouchingLetterChangedListener;
        final int c = (int) (y / getHeight() * letter.size());// 点击y坐标所占总高度的比例*b数组的长度就等于点击b中的个数

        switch (action) {
            case MotionEvent.ACTION_UP:
                setBackground(new ColorDrawable(0x00000000));
                choose = -1;//
                invalidate();
                if (mTextDialog != null) {
                    mTextDialog.setVisibility(View.INVISIBLE);
                }
                break;

            default:
//			setBackgroundResource(R.drawable.sidebar_background);
                setBackground(new ColorDrawable(0x33333333));
                if (oldChoose != c) {
                    if (c >= 0 && c < letter.size()) {
                        if (listener != null) {
                            listener.onTouchingLetterChanged(letter.get(c));
                            LogeUtil.e("getLetter", "c==" + c + "letter.get(c)=" + letter.get(c));
                        }
                        if (mTextDialog != null) {
                            mTextDialog.setText(letter.get(c));
                            mTextDialog.setVisibility(View.VISIBLE);
                        }

                        choose = c;
                        invalidate();
                    }
                }

                break;
        }
        return true;
    }

    /**
     * 触摸事件
     *
     * @param onTouchingLetterChangedListener
     */
    public void setOnTouchingLetterChangedListener(
            OnTouchingLetterChangedListener onTouchingLetterChangedListener) {
        this.onTouchingLetterChangedListener = onTouchingLetterChangedListener;
    }

    /**
     * @author coder
     */
    public interface OnTouchingLetterChangedListener {
        void onTouchingLetterChanged(String s);
    }

}